package com.ld.zxw.index;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NoLockFactory;

import com.ld.zxw.config.Config;
import com.ld.zxw.config.IndexFactory;
import com.ld.zxw.util.CommonUtil;
import com.ld.zxw.util.DateUtil;
public class AddIndex implements IndexFactory{


	private Logger log = Logger.getLogger(AddIndex.class);
	
	@Override
	public boolean saveIndexs(Config config,List<Document> list) {
		long start_time = DateUtil.getTime();
		IndexWriter  indexWriter = null;
		FSDirectory fsDirectory = null;
		try {
			fsDirectory = FSDirectory.open(config.getPath(), NoLockFactory.INSTANCE);
			indexWriter = new IndexWriter(fsDirectory, new IndexWriterConfig(config.getAnalyzer()));
			indexWriter.addDocuments(list);
			long commit = indexWriter.commit();
			DateUtil.timeConsuming("添加内核", start_time);
			log.info("提交返回:"+commit);
			return true;
		} catch (IOException e) {
			log.error("indexWriter.commit 失败", e);
			return false;
		}finally {
			CommonUtil.colseIndexWriter(indexWriter);
			CommonUtil.colseDirectory(fsDirectory);
		}
	}
}
